<?php

/**
 * RoleController.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysmanger\Controller;

use System\Common\SystemController;

class RoleController extends SystemController {
    public $limit = 20;
    public function index()
    {
        $this->system_title = '角色管理';
        
        $page = rintval(I('get.p', 1));
        $start = null;
        $limit = $this->limit;
        list($start, $limit, $page) = page_limit($page, $limit);
        
        $params = array(
                'fields'=>'role_id,role_name,created',
                'start'=> $start,
                'limit'=>$limit,
                'order'=>'role_id desc',
        );
        $obj_serv = D('Role','Service');
        $result = $obj_serv->getRoleList($params);
        
        $count = 0;
        $list = array();
        if($result)
        {
            $this->_assignPage($result['count'], $limit);
            $count = $result['count'];
            $list = $result['list'];
        }
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->loadSystemView();
    }

    public function add()
    {
        $this->system_title = '添加角色';
        $per_list = $this->_getPermissonList();
        $this->assign('act_url', U('Role/dosubmit'));
        $this->assign('per_list', $per_list);
        $this->loadSystemView('edit');
    }

    public function edit()
    {
        $this->system_title = '编辑角色';
        $role_id = I('get.role_id');
        if(!rintval($role_id))
        {
            return $this->error('角色不存在');
        }
        
        $obj_manger_serv = D('Manger', 'Service');
        $role_info = $obj_manger_serv->getRole($role_id, 'role_name,permission');
        if(!$role_info)
        {
            return $this->error('角色不存在');
        }
        $role_per_list = explode(',', $role_info['permission']);
        $per_list = $this->_getPermissonList();
        $role_info['role_id'] = $role_id;
        
        $this->assign('act_url', U('Role/dosubmit'));
        $this->assign('per_list', $per_list);
        $this->assign('data', $role_info);
        $this->assign('role_per_list', $role_per_list);
        
        $this->loadSystemView();
    }

    public function del()
    {
        $role_id = I('get.role_id');
        try {
            $obj_serv = D('Role','Service');
            $rs = $obj_serv->delRole(intval($role_id));
            if(!$rs)
            {
                throw new \Exception('删除失败');
            }
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
        
        return $this->success('删除成功',U('Role/index'));
    }

    public function dosubmit()
    {
        $request = I('post.');
        
        $data['role_name'] = $request['role_name'];
        $data['permission'] = implode(',', $request['permission']);
        if(isset($request['role_id']))
        {
            $data['role_id'] = $request['role_id'];
        }
        
        try {
            $obj_serv = D('Role', 'Service');
            $rs = $obj_serv->saveRole($data);
            if(!$rs)
            {
                throw new \Exception('保存失败，请重试');
            }
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
        
        return $this->success('保存成功', U('Role/index'));
    }
    
    

}
